package demo2
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	import nape.geom.Vec2;
	import nape.phys.Body;
	import nape.phys.BodyType;
	import nape.shape.Polygon;
	import nape.space.Space;
	import nape.util.ShapeDebug;

	/**
	 * 测试Nape 贴图(使用PNG贴图 or 矢量贴图)
	 */
	public final class DemoScene_2 extends Sprite
	{
		private var space:Space;
		private var uiContainer:MovieClip;

		private var allSceneImageBoxVector:Vector.<ImageSkinBox>;

		private var debug:ShapeDebug;

		public function DemoScene_2()
		{
			super();
			this.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);

			allSceneImageBoxVector=new Vector.<ImageSkinBox>();
		}

		protected function onAddToStage(event:Event):void
		{
			var gravity:Vec2=new Vec2(0, 600);
			space=new Space(gravity);

			uiContainer=ResUtils.getMC("DemoScene2") as MovieClip;
			this.addChild(uiContainer);

			NapeUtils.createStaticBoxBodyByMc(uiContainer["floorMc"], space);

			//3.创建模拟视图
			debug=new ShapeDebug(this.stage.width, this.stage.height);
			this.addChild(debug.display);

			this.addEventListener(Event.ENTER_FRAME, loop);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseEventHandler);
		}

		private function mouseEventHandler(e:MouseEvent):void
		{
			var imageBox:ImageSkinBox=new ImageSkinBox();
			imageBox.initialize(uiContainer, this.mouseX, this.mouseY, space);
			allSceneImageBoxVector.push(imageBox);
		}

		private function loop(e:Event):void
		{
			//Nape空间模拟
			space.step(1 / 60, 30);

//			//清除视图
//			debug.clear();
//			//优化显示图像
//			debug.flush();
//			//绘制空间
//			debug.draw(space);


			for (var i:int=0; i < space.liveBodies.length; i++)
			{
				var imageBox:ImageSkinBox=space.liveBodies.at(i).userData["target"];
				imageBox && imageBox.adjustDisplay();
			}
		}

	}
}
